package com.yyfax.account.controller.api;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.jaws.core.common.log.Log;
import com.jaws.core.common.vo.OpRequest;
import com.jaws.core.common.vo.OpResponse;
import com.yyfax.account.common.enums.AccountTypeEnum;
import com.yyfax.account.common.exception.AccountExceptionCode;
import com.yyfax.account.service.AccountQueryService;
import com.yyfax.account.vo.BatchUserQueryReq;
import com.yyfax.account.vo.PlatformQueryReq;
import com.yyfax.account.vo.UserQueryFlowReq;
import com.yyfax.account.vo.UserQueryReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 账户查询API
 * 
 * @author maojian
 * @date 2017-05-23 16:02:06
 * @since v1.0.0
 */
@RestController
public class AccountQueryApiController {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	private AccountQueryService accountQueryService;

	/**
	 * 根据用户ID查询账户信息
	 * 
	 * @author maojian
	 * @date 2017-05-26 09:01:00
	 * @since v1.0.0
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/account/query_account_by_user")
	public OpResponse queryAccountInfoByUser(@RequestBody @Valid OpRequest<UserQueryReq> req) throws Exception {
		logger.info(Log.op("AccountQueryApiController.queryAccountInfoByUser").msg("enter").kv("req", req).toString());
		OpResponse response = OpResponse.success();
		response.setContent(accountQueryService.queryAccountInfoByUser(req.getParams().getUserId()));
		return response;
	}

	/**
	 * 根据用户ID查询账户信息(批量)
	 * 
	 * @author maojian
	 * @date 2017-05-26 09:01:00
	 * @since v1.0.0
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/account/batch/query_account_by_user")
	public OpResponse queryAccountInfoListByUsers(@RequestBody @Valid OpRequest<BatchUserQueryReq> req) throws Exception {
		logger.info(Log.op("AccountQueryApiController.queryAccountInfoListByUsers").msg("enter").kv("req", req).toString());
		OpResponse response = OpResponse.success();
		List<String> userIds = Lists.transform(req.getParams().getUserQueryReqs(), UserId.INSTANCE);
		response.setContent(accountQueryService.queryAccountInfoListByUsers(userIds));
		return response;
	}

	enum UserId implements Function<UserQueryReq, String> {
		INSTANCE;
		@Override
		public String apply(UserQueryReq userQuery) {
			return userQuery.getUserId();
		}
	}

	/**
	 * 根据用户ID查询账户流水
	 * 
	 * @author maojian
	 * @date 2017-05-26 09:01:00
	 * @since v1.0.0
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/account/query_account_flow_by_user")
	public OpResponse queryAccountFlowByUser(@RequestBody @Valid OpRequest<UserQueryFlowReq> req) throws Exception {
		logger.info(Log.op("AccountQueryApiController.queryAccountFlowByUser").msg("enter").kv("req", req).toString());
		OpResponse response = OpResponse.success();
		response.setContent(accountQueryService.queryAccountFlowByUser(req.getParams()));
		return response;
	}

	/**
	 * 根据平台类型查询账户信息
	 * 
	 * @author maojian
	 * @date 2017-05-26 09:01:00
	 * @since v1.0.0
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/account/query_platform_account")
	public OpResponse queryPlatformAccount(@RequestBody @Valid OpRequest<PlatformQueryReq> req) throws Exception {
		logger.info(Log.op("AccountQueryApiController.queryPlatformAccount").msg("enter").kv("req", req).toString());

		if (!req.getParams().getAccountType().isPlatformAccount()) {
			throw AccountExceptionCode.NOT_PLATFORM.exp();
		}
		OpResponse response = OpResponse.success();
		response.setContent(accountQueryService.queryPlatformAccount(req.getParams().getAccountType()));
		return response;
	}

	/**
	 * 查询平台所有的账户类型
	 * 
	 * @author buyi
	 * @date 2017-09-26 10:26:12
	 * @since v2.0.0
	 * @param req
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/account/query_platform_account_all")
	public OpResponse queryAllPlatformAccount(@RequestBody OpRequest<?> req) {
		logger.info(Log.op("AccountQueryApiController.queryPlatformAccount").msg("enter").kv("req", req).toString());

		OpResponse response = OpResponse.success();
		response.setContent(accountQueryService.queryAllPlatformAccount(AccountTypeEnum.getPlatformAccountTyps()));
		return response;
	}

}
